using System;
using System.Data;
using DayPilot.Web.Ui.Enums;

public partial class Scheduler_Month : System.Web.UI.Page
{
    private DataTable table;

    protected void Page_Load(object sender, EventArgs e)
    {
        #region Data loading initialization
        if (Session["Month"] == null)
        {
            Session["Month"] = DataGenerator.GetData();
        }
        table = (DataTable)Session["Month"];
        DayPilotScheduler1.DataSource = Session["Month"];
        DayPilotNavigator1.DataSource = Session["Month"];
        #endregion

        

        if (!IsPostBack)
        {
            DateTime start = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
            DateTime end = start.AddMonths(1);

            DayPilotScheduler1.StartDate = start;
            DayPilotScheduler1.Days = (int)(end - start).TotalDays;

            DataBind();
        }
    }

    protected void DayPilotScheduler1_TimeRangeSelected(object sender, DayPilot.Web.Ui.Events.TimeRangeSelectedEventArgs e)
    {
        #region Simulation of database update

        DataRow dr = table.NewRow();
        dr["start"] = e.Start;
        dr["end"] = e.End;
        dr["id"] = Guid.NewGuid().ToString();
        dr["name"] = "New event";
        dr["column"] = e.ColumnId;

        table.Rows.Add(dr);
        table.AcceptChanges();
        #endregion

        DayPilotScheduler1.DataBind();
        DayPilotScheduler1.Update();

    }
    protected void DayPilotScheduler1_EventMenuClick(object sender, DayPilot.Web.Ui.Events.EventMenuClickEventArgs e)
    {

        if (e.Command == "Delete")
        {
            #region Simulation of database update
            DataRow dr = table.Rows.Find(e.Value);

            if (dr != null)
            {
                table.Rows.Remove(dr);
                table.AcceptChanges();
            }
            #endregion

            DayPilotScheduler1.DataBind();
            DayPilotScheduler1.Update();
        }
    }

    protected void DayPilotScheduler1_TimeRangeMenuClick(object sender, DayPilot.Web.Ui.Events.TimeRangeMenuClickEventArgs e)
    {
        if (e.Command == "Insert")
        {
            #region Simulation of database update

            DataRow dr = table.NewRow();
            dr["start"] = e.Start;
            dr["end"] = e.End;
            dr["id"] = Guid.NewGuid().ToString();
            dr["name"] = "New event";
            dr["column"] = e.ResourceId;

            table.Rows.Add(dr);
            table.AcceptChanges();
            #endregion

            DayPilotScheduler1.DataBind();
            DayPilotScheduler1.Update();
            
        }
    }
    protected void DayPilotScheduler1_Command(object sender, DayPilot.Web.Ui.Events.CommandEventArgs e)
    {
        switch (e.Command)
        {
            case "navigate":
                DateTime start = (DateTime)e.Data["start"];
                DateTime end = (DateTime)e.Data["end"];
                int days = (int)e.Data["days"];

                DayPilotScheduler1.StartDate = start;
                DayPilotScheduler1.Days = days;
                DayPilotScheduler1.DataBind();
                DayPilotScheduler1.Update(CallBackUpdateType.Full);
                break;
        }

    }
    protected void DayPilotNavigator1_VisibleRangeChanged(object sender, DayPilot.Web.Ui.Events.Navigator.VisibleRangeChangedEventArgs e)
    {
        DayPilotNavigator1.DataBind();
    }
}
